/******************************************************************************/
package jamie.org.sil.www.usxConverter;


import jamie.org.sil.www.usxUtils.*;



/******************************************************************************/
/**
   A USFM-to-USX converter.
   <p>

   This tool takes "standard" USFM files, and does two things.  It
   <i>always</i> parses them and checks that they comply with the standard
   (or more accurately, with our interpretation of the standard); and it
   <i>can</i> also convert them to USX.
   <p>

   It's your choice whether to generate the USX output or not, and
   you'll get it only where the parsing is "reasonably" successful, a
   somewhat enigmatic expression which needs a little further
   elaboration.
   <p>

   Paratext (which is the source of my knowledge about how to parse
   USFM) is quite strict in many respects -- there are quite a lot of
   things you can do which are wrong in its eyes, and I do my best to
   report on all of them.  However, not all of them actually have any
   bearing on whether or not I can successfully generate USX.  To
   take an example, Paratext demands that certain tags come at the
   start of a line, and I report on those which do not.  However, I
   can arrive at precisely the same USX regardless of whether the
   tags come at the start of a line or not, so this "error" has no
   bearing on USX generation.  In contrast, a verse marker with no
   number is both an error as far as Paratext is concerned and would
   result in me generating invalid USX, so in this case I don't
   generate any.
*/

public class UsxConverterFromUsfm extends UsxConverter
{
  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                               Public                                   **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /****************************************************************************/
  /**
     Runs over all input files doing processing.

     @throws Exception
   */

  @Override public void process () throws Exception
  {
     try
     {
       processFiles();
     }
     catch (ConverterExceptionAbandonRun e)
     {
       throw e;
     }
  }

  
  
  
  
  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                               Private                                  **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /****************************************************************************/
  /* Processes all files. */
  
  private void processFiles () throws Exception
  {
    BookElement be = null;
    BookAssemblyAllFilesInInputFolderIterator it = new BookAssemblyAllFilesInInputFolderIterator();
    while (it.hasNext())
      try
      {
        IncidentHandler.instance().resetForNewFile();
        be = (BookElement) it.next();
        UsxConverterFromUsfm_Parser parser = new UsxConverterFromUsfm_Parser();
        LogManager.instance().setPrefix(be.getInputFileName());
        parser.processFile(be.getInputPathName());
        //LogManager.instance().setPrefix("");
        IncidentHandler.instance().handleAnyDeferredAction();
      }
      catch (ConverterExceptionAbandonFile e)
      {
        Utils.deleteFile(be.getOutputFileName(0));  
      }
      finally
      {
        LogManager.instance().setPrefix("");
      }
  }
}
